import axios from 'axios'
// import { getStorage } from '@/utils/storage.js'
import store from '@/store'
import JSONbig from 'json-bigint'

const request = axios.create({
  baseURL: 'http://toutiao-app.itheima.net',
  transformResponse: [function(data) {
    // 对 data 进行任意转换处理
    try {
      return JSONbig.parse(data)
    } catch (error) {
      return data
    }
  }],
  timeout: 900
})

// 请求拦截器
request.interceptors.request.use(config => {
  if (store.state.user) {
    config.headers.Authorization = 'Bearer ' + store.state.user.token
  }
  return config
})

// 响应拦截器
request.interceptors.response.use(res => {
  // console.log(res)
  return res.data.data
})

export default request

// export const request2 = axios.create({
//   baseURL: 'http://toutiao-app.itheima.net',
//   headers: {
//     Authorization: 'Bearer ' + getStorage('tt-user').token
//   }
// })
